package com.lv.common.widget.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.lv.common.R
import com.lv.common.imp.OnViewClickListener
import com.lv.common.ktx.LoadSrc

/**
 * 作者： 志威  zhiwei.org
 * 主页： Github: https://github.com/zhiwei1990
 * 日期： 2020年10月08日 14:24
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/  -- 志威 zhiwei.org
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 自定义的设置item的组合控件
 */
class TitleBarView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private var mIvBackPage: AppCompatImageView? = null
    private var mIvRightPage: AppCompatImageView? = null
    private var mTvRightPage: AppCompatTextView? = null
    private var mTvTitlePage: AppCompatTextView? = null
    private var mIvTitle: ImageView? = null
    var rightTitleListener: OnViewClickListener? = null
    private var backIconListener: View.OnClickListener? = null
    private var rightIconListener: View.OnClickListener? = null

    init {

        val inflate = inflate(context, R.layout.v_title_bar, this)
        mIvBackPage = inflate.findViewById<AppCompatImageView>(R.id.iv_back_page)
        mTvTitlePage = inflate.findViewById<AppCompatTextView>(R.id.tv_title_page)
        mTvRightPage = inflate.findViewById<AppCompatTextView>(R.id.tv_right_page)
        mIvRightPage = inflate.findViewById<AppCompatImageView>(R.id.iv_right_page)
        mIvTitle = inflate.findViewById<ImageView>(R.id.iv_image_title)
//        setBackgroundColor(Color.WHITE)
        mTvTitlePage?.paint?.isFakeBoldText = true
        mTvRightPage?.setOnClickListener { rightTitleListener?.onClick() }
        mIvRightPage?.setOnClickListener { rightIconListener?.onClick(this) }
        mIvBackPage?.setOnClickListener { backIconListener?.onClick(this) }

        // region  2、读取配置属性
//        val attributes = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView).apply {
//            //icon 设置
//            itemBean.iconRes =
//                getResourceId(R.styleable.TitleBarView_icon, R.drawable.icon_back)
//            val iconRGB = getColor(R.styleable.TitleBarView_iconColor, 0)
//            itemBean.iconColor = iconRGB
//            //title设置
//            itemBean.title = getString(R.styleable.TitleBarView_title) ?: ""
//            val titleRGB = getColor(
//                R.styleable.TitleBarView_titleColor,
//                resources.getColor(R.color.color_242424)
//            )
//            itemBean.titleColor = titleRGB
//            //desc设置
//            itemBean.desc = getString(R.styleable.TitleBarView_desc) ?: ""
//            val descRGB = getColor(
//                R.styleable.TitleBarView_descColor,
//                resources.getColor(R.color.color_242424)
//            )
//            itemBean.descColor = descRGB
//            //arrow设置
//            itemBean.arrowRes =
//                getResourceId(R.styleable.TitleBarView_arrow, R.drawable.icon_back)
//            val arrowRGB = getColor(
//                R.styleable.TitleBarView_arrowColor,
//                resources.getColor(R.color.colorSecondaryText)
//            )
//            itemBean.arrowColor = arrowRGB
//        }

        // 回收 recycle
//        attributes.recycle()
        // endregion

    }

    //region 设置资源


    /**
     * 设置title
     */
    fun setTitle(title: String) {
        mTvTitlePage?.text = title
    }

    /**
     * 设置title
     */
    fun setTitleIv(iv: Int) {
        mIvTitle?.visibility = View.VISIBLE
        mIvTitle?.LoadSrc(iv)
    }

    /**
     * 设置右侧内容描述
     */
    fun setRightBarDec(
        desc: String,
        isShow: Boolean = false,
        color: String = "#242424",
        listener: OnViewClickListener? = null
    ) {
        mTvRightPage?.text = desc
        if (isShow) {
            mTvRightPage?.visibility = VISIBLE
        } else {
            mTvRightPage?.visibility = GONE
        }
        mTvRightPage?.setTextColor(Color.parseColor(color))
        rightTitleListener = listener
    }

    /**
     * 设置右侧标题显示
     */
    fun setRightBarDec(isShow: Boolean) {
        if (isShow) {
            mTvRightPage?.visibility = VISIBLE
        } else {
            mTvRightPage?.visibility = GONE
        }
    }

    /**
     * 设置右侧图标是否显示
     */
    fun setRightBarIconShow(isShow: Boolean) {
        if (isShow) {
            mIvRightPage?.visibility = VISIBLE
        } else {
            mIvRightPage?.visibility = GONE
        }
    }

    /**
     * 设置左侧图标是否显示
     */
    fun setBackIconShow(isShow: Boolean) {
        if (isShow) {
            mIvBackPage?.visibility = VISIBLE
        } else {
            mIvBackPage?.visibility = GONE
        }
    }

    /**
     * 设置icon图标
     */
    fun setRightIcon(iconRes: Int, show: Boolean = true) {
        mIvRightPage?.LoadSrc(iconRes)
        mIvRightPage?.visibility = if (show) VISIBLE else GONE
    }

    /**
     * 设置icon图标
     */
    fun setLeftIcon(iconRes: Int, show: Boolean = true) {
        mIvBackPage?.LoadSrc(iconRes)
        mIvBackPage?.visibility = if (show) VISIBLE else GONE
    }


    //endregion

    //region 点击事件

    /**
     * 单独点击图标
     */
    fun onRightIcon(listener: OnClickListener) {
        rightIconListener = listener
    }

    /**
     * 单独点击title
     */
    fun onClickRightTitle(listener: OnViewClickListener) {
        rightTitleListener = listener
    }

    /**
     * 单独点击title
     */
    fun onClickBackIcon(listener: OnClickListener) {
        backIconListener = listener
    }


    //endregion

    //region 设置颜色

    /* */
    /**
     * 设置标题title颜色
     *//*
    fun setIconColor(colorRes: Int) {
        itemBean.iconColor = colorRes
    }

    */
    /**
     * 设置标题title颜色
     */
    fun setTitleColor(colorRes: Int) {
        mTvTitlePage?.setTextColor(colorRes)
    }


    /**
     * 设置标题title颜色
     *//*
    fun setRightTitleColor(colorRes: Int) {
        itemBean.descColor = colorRes
    }

    */
    /**
     * 设置标题title颜色
     *//*
    fun setArrowColor(colorRes: Int) {
        itemBean.arrowColor = colorRes
    }
*/
    //endregion

//    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
//        return hasOnClickListeners()
//    }

}